﻿using System;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraSplashScreen;
using Sumen.Business;
using Sumen.Business.Factory;
using Sumen.Library.Classes.Common;
using Sumen.Model.Model;

namespace Sumen.Main
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                

#if (Debug)
            ClassLib.Classes.Connection.ClsConnection.Server = "localhost";
            ClassLib.Classes.Connection.ClsConnection.Database = "sumen";
            ClassLib.Classes.Connection.ClsConnection.Username = "root";
            ClassLib.Classes.Connection.ClsConnection.Password = "123456";
#endif

                int i = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
                if (i == 1)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    DevExpress.Skins.SkinManager.EnableFormSkins();

                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);
                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    Application.SetCompatibleTextRenderingDefault(false);



                    ClsPublic.GetListParameters();
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(ClsPublic.SYSTEM_CULTURE);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(ClsPublic.SYSTEM_CULTURE);
                    Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = ClsPublic.SYSTEM_FORMAT_DATE;
                    System.Globalization.DateTimeFormatInfo.CurrentInfo.DateSeparator = "/";
                    float fontSize = 8.25f;
                    try
                    {
                        fontSize = Convert.ToSingle(ClsPublic.SYSTEM_FONT_SIZE);
                    }
                    catch (Exception)
                    {
                        fontSize = 8.25f;
                    }
                    DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font(ClsPublic.SYSTEM_FONT_NAME, fontSize);

#if (DEBUG)
                    ClsPublic.User = new user() { User_Username = "admin" };
                    FormManager formManager = FormManager.getInstance();

                    if (formManager._RibbonForm == null || formManager._RibbonForm.IsDisposed)
                        //objFrmMain = new FrmMain_CTU();
                        formManager._RibbonForm = new global::Sumen.Main.Main();

                    formManager._RibbonForm.Show();
                    formManager._RibbonForm.Activate();

                    

                    Application.Run(formManager._RibbonForm);

#else
                    // Application.Run(new Main());
                    Application.Run(new FrmDangNhap());     
#endif


                }
                else
                {
                    //MessageBox.Show("Chỉ có thể mở một thể hiện của chương trình này.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //Application.Exit();
                }
                //Thread.CurrentThread.CurrentCulture = new CultureInfo(CultureInfo.CurrentCulture.Name);
                //Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
                //System.Globalization.DateTimeFormatInfo.CurrentInfo.DateSeparator = "/";
            }
            catch (Exception ex)
            {
            }

        }
        private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {

            HandleException(e.ExceptionObject as Exception);
        }
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception as Exception);
        }

        private static void UnKnownExceptions(string strError, string detailStrError)
        {
            try
            {
                //ShellEntities context = new ShellEntities();
                //UnKnownException exception = new UnKnownException();
                //exception.DateTimeError = DateTime.Now;
                //exception.ErrorText = strError;
                //exception.DetailErrorText = detailStrError;
                //context.AddToUnKnownException(exception);
                //context.SaveChanges();
            }
            catch
            {

            }
        }


        private static void HandleException(Exception ex)
        {

            SplashScreenManager.ActivateParentOnSplashFormClosing = false;
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch
            {
            }
            try
            {
                EntityFactory.getInstance().rollBack();
            }
            catch
            {
            }
            try
            {
                EntityFactory.getInstance().rollBackSystem();
            }
            catch
            {
            }
            if (ClsMsgBox.LoiChung(ex, false) == 1)
            {
                ClsPublic.WriteException(ex);
            }
        }







    }
}
